package com.gxuwz.medical.domain.register;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.gxuwz.medical.database.DBUtil;
import com.gxuwz.medical.domain.family.Family;

/**
 * 参合登记模块类
 * @author 沉羽墨玖
 *
 */
public class Register {
	private String persCode; // 参合证号
	private Family family;
	private String persName; // 姓名
	private String cardId; // 身份证号
	private String sex; // 性别
	private int age; // 年龄
	private String illName; //疾病名称
	private String liveAddress; // 家庭住址
	private String telephone; // 联系方式
	private String createTime; // 参合时间
	private String invoice; // 参合发票号

	public Register() {

	}

	public Register(String persCode) throws SQLException {
		this.persCode = persCode;
		this.load();
	}

	public Register(String persCode, Family family, String persName,
			String cardId, String sex, int age,String illName,String liveAddress,
			String telephone, String createTime, String invoice) {
		super();
		this.persCode = persCode;
		this.family = family;
		this.persName = persName;
		this.cardId = cardId;
		this.sex = sex;
		this.age = age;
		this.illName = illName;
		this.liveAddress = liveAddress;
		this.telephone = telephone;
		this.createTime = createTime;
		this.invoice = invoice;
	}

	public void load() throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.getConn();
			String sql = "select * from register where persCode=?";
			pstmt = conn.prepareStatement(sql);
			int index = 1;
			pstmt.setString(index, this.persCode);
			rs = pstmt.executeQuery();
			rs.next();
			this.persCode = rs.getString("persCode");
			//this.setFamily(family);
			this.persName = rs.getString("persName");
			this.cardId = rs.getString("cardId");
			this.sex = rs.getString("sex");
			this.age = rs.getInt("age");
			this.illName = rs.getString("illName");
			this.liveAddress = rs.getString("liveAddress");
			this.telephone = rs.getString("telephone");
			this.createTime = rs.getString("createTime");
			this.invoice = rs.getString("invoice");
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}

	}

	/**
	 * 添加参合登记
	 * 
	 * @throws Exception
	 */
	public void add() throws Exception {
		Connection conn = null;
		try {
			conn = DBUtil.getConn();
			conn.setAutoCommit(false);
			saveToDB(conn);
			conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			conn.rollback();
			throw e;
		} finally {
			DBUtil.close(conn);
		}
	}

	private void saveToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		// 定义日期格式为yyyyMMdd
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
		int i = (int) (Math.random() * 1000) + 1; // 随机数
		String id = sf.format(new Date()) + i;
		try {
			StringBuffer sqlBuff = new StringBuffer();
			sqlBuff.append("insert into register(persCode,famCode,persName,cardId,sex,age,illName,liveAddress,telephone,createTime,invoice)");
			sqlBuff.append("values(?,?,?,?,?,?,?,?,?,?,?)");
			String sql = sqlBuff.toString();
			pstmt = conn.prepareStatement(sql);
			int index = 1;
			pstmt.setString(index++, this.persCode);
			pstmt.setString(index++, this.family.getFamCode());
			pstmt.setString(index++, this.persName);
			pstmt.setString(index++, this.cardId);
			pstmt.setString(index++, this.sex);
			pstmt.setInt(index++, this.age);
			pstmt.setString(index++, this.illName);
			pstmt.setString(index++, this.liveAddress);
			pstmt.setString(index++, this.telephone);
			pstmt.setString(index++, this.createTime);
			pstmt.setString(index++, id);
			int count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtil.close(pstmt);
		}
	}

	/**
	 * 删除参合登记信息
	 * 
	 * @param persCode
	 * @throws SQLException
	 */
	public void del(String persCode) throws SQLException {
		Connection conn = null;
		this.persCode = persCode;
		try {
			conn = DBUtil.getConn();
			conn.setAutoCommit(false);
			// 调用delFromDB方法
			deleteFromDB(conn);
			// 提交事务
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DBUtil.close(conn);
		}

	}

	/**
	 * 删除参合登记信息
	 * 
	 * @param conn
	 * @throws SQLException
	 */
	private void deleteFromDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer("delete from register where persCode=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.persCode);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("Failed to delete register from table !", e);
		} finally {
			DBUtil.close(pstmt);

		}

	}

	public String getPersCode() {
		return persCode;
	}

	public void setPersCode(String persCode) {
		this.persCode = persCode;
	}

	public String getCardId() {
		return cardId;
	}

	public void setCardId(String cardId) {
		this.cardId = cardId;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getInvoice() {
		return invoice;
	}

	public void setInvoice(String invoice) {
		this.invoice = invoice;
	}

	public String getPersName() {
		return persName;
	}

	public void setPersName(String persName) {
		this.persName = persName;
	}

	public String getLiveAddress() {
		return liveAddress;
	}

	public void setLiveAddress(String liveAddress) {
		this.liveAddress = liveAddress;
	}
	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public Family getFamily() {
		return family;
	}

	public void setFamily(Family family) {
		this.family = family;
	}

	public String getIllName() {
		return illName;
	}

	public void setIllName(String illName) {
		this.illName = illName;
	}

}
